
package com.borqs.music.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;

import com.borqs.music.model.SingerInfo;
import com.borqs.music.model.SongInfo;

public class CacheUtils {
    public static final int CACHE_TYPE_HOT = -1;
    public static final int CACHE_TYPE_LATEST = -2;
    public static final int CACHE_TYPE_SINGER = -3;
    public static final int CACHE_TYPE_SHARED = -4;
    public static final int CACHE_TYPE_CATEGORY = -5;
    public static final int CACHE_TYPE_PERSONAL_SONG = -10;

    public static class SongCache {
        public static void put(Context context, int type, List<SongInfo> songs) {
            if (songs == null || songs.size() == 0) {
                return;
            }

            MusicDatabaseHelper helper = new MusicDatabaseHelper(context);
            SQLiteDatabase db = helper.getWritableDatabase();
            String whereClause = SongCacheInfo.COLUMN_CACHE_TYPE + " = " + type;
            // delete old cache
            db.delete(SongCacheInfo.NAME, whereClause, null);

            db.beginTransaction();
            try {
                ContentValues values = new ContentValues();
                for (SongInfo song : songs) {
                    values.clear();
                    values.put(SongCacheInfo.COLUMN_SONG_ID, song.id);
                    values.put(SongCacheInfo.COLUMN_FILE_NAME, song.fileName);
                    values.put(SongCacheInfo.COLUMN_FILE_TYPE, song.fileType);
                    values.put(SongCacheInfo.COLUMN_FILE_URL, song.fileUrl);
                    values.put(SongCacheInfo.COLUMN_FILE_SIZE, song.fileSize);
                    values.put(SongCacheInfo.COLUMN_FILE_MD5, song.fileMd5);
                    values.put(SongCacheInfo.COLUMN_TITLE, song.title);
                    values.put(SongCacheInfo.COLUMN_ARTIST, song.artist);
                    values.put(SongCacheInfo.COLUMN_ALBUM, song.album);
                    values.put(SongCacheInfo.COLUMN_YEAR, song.year);
                    values.put(SongCacheInfo.COLUMN_GENRE, song.genre);
                    values.put(SongCacheInfo.COLUMN_LYRICS, song.lyrics);
                    values.put(SongCacheInfo.COLUMN_IMG_URL, song.imgUrl);
                    values.put(SongCacheInfo.COLUMN_IS_FAVORITE, song.isFavorite);
                    values.put(SongCacheInfo.COLUMN_IS_NOT_IN_DEVICE, song.isNotInDevice);
                    values.put(SongCacheInfo.COLUMN_IS_PUBLIC_SONG, song.isPublicSong);
                    values.put(SongCacheInfo.COLUMN_BITRATE, song.bitrate);
                    values.put(SongCacheInfo.COLUMN_DURATION, song.duration);
                    values.put(SongCacheInfo.COLUMN_CACHE_TYPE, type);

                    db.insert(SongCacheInfo.NAME, "0", values);
                }
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }

            db.close();
        }

        public static void remove(Context context, int type, int songId) {
            if (songId < 0) {
                return;
            }

            MusicDatabaseHelper helper = new MusicDatabaseHelper(context);
            SQLiteDatabase db = helper.getWritableDatabase();
            String whereClause = SongCacheInfo.COLUMN_SONG_ID + " = " + songId;
            // delete old cache
            db.delete(SongCacheInfo.NAME, whereClause, null);
            db.close();
        }

        public static List<SongInfo> get(Context context, int type) {
            ArrayList<SongInfo> songs = new ArrayList<SongInfo>();
            MusicDatabaseHelper helper = new MusicDatabaseHelper(context);
            SQLiteDatabase db = helper.getWritableDatabase();

            SQLiteQueryBuilder sqlbuilder = new SQLiteQueryBuilder();
            sqlbuilder.setTables(SongCacheInfo.NAME);

            String selection = SongCacheInfo.COLUMN_CACHE_TYPE + " = " + type;
            Cursor cursor = sqlbuilder.query(db, null /* projectionIn */, selection,
                    null/* selectionArgs */,
                    null/* groupBy */, null/* having */, null/* sortOrder */);

            if (cursor != null) {
                int idIndex = cursor.getColumnIndexOrThrow(SongCacheInfo.COLUMN_SONG_ID);
                int fileNameIndex = cursor.getColumnIndexOrThrow(SongCacheInfo.COLUMN_FILE_NAME);
                int fileTypeIndex = cursor.getColumnIndexOrThrow(SongCacheInfo.COLUMN_FILE_TYPE);
                int fileUrlIndex = cursor.getColumnIndexOrThrow(SongCacheInfo.COLUMN_FILE_URL);
                int fileSizeIndex = cursor.getColumnIndexOrThrow(SongCacheInfo.COLUMN_FILE_SIZE);
                int fileMD5 = cursor.getColumnIndexOrThrow(SongCacheInfo.COLUMN_FILE_MD5);
                int titleIndex = cursor.getColumnIndexOrThrow(SongCacheInfo.COLUMN_TITLE);
                int artistIndex = cursor.getColumnIndexOrThrow(SongCacheInfo.COLUMN_ARTIST);
                int albumIndex = cursor.getColumnIndexOrThrow(SongCacheInfo.COLUMN_ALBUM);
                int yearIndex = cursor.getColumnIndexOrThrow(SongCacheInfo.COLUMN_YEAR);
                int genreIndex = cursor.getColumnIndexOrThrow(SongCacheInfo.COLUMN_GENRE);
                int lyricsIndex = cursor.getColumnIndexOrThrow(SongCacheInfo.COLUMN_LYRICS);
                int imgUrlIndex = cursor.getColumnIndexOrThrow(SongCacheInfo.COLUMN_IMG_URL);
                int isFavoriteIndex = cursor
                        .getColumnIndexOrThrow(SongCacheInfo.COLUMN_IS_FAVORITE);
                int isNotInDeviceIndex = cursor
                        .getColumnIndexOrThrow(SongCacheInfo.COLUMN_IS_NOT_IN_DEVICE);
                int isPublicSongIndex = cursor
                        .getColumnIndexOrThrow(SongCacheInfo.COLUMN_IS_PUBLIC_SONG);
                int bitrateIndex = cursor.getColumnIndexOrThrow(SongCacheInfo.COLUMN_BITRATE);
                int durationIndex = cursor.getColumnIndexOrThrow(SongCacheInfo.COLUMN_DURATION);

                while (cursor.moveToNext()) {
                    SongInfo song = new SongInfo();
                    song.id = cursor.getInt(idIndex);
                    song.fileName = cursor.getString(fileNameIndex);
                    song.fileType = cursor.getString(fileTypeIndex);
                    song.fileUrl = cursor.getString(fileUrlIndex);
                    song.fileSize = cursor.getLong(fileSizeIndex);
                    song.fileMd5 = cursor.getString(fileMD5);

                    // ID3 Tags
                    song.title = cursor.getString(titleIndex);
                    song.artist = cursor.getString(artistIndex);
                    song.album = cursor.getString(albumIndex);
                    song.year = cursor.getString(yearIndex);
                    song.genre = cursor.getString(genreIndex);
                    song.lyrics = cursor.getString(lyricsIndex);
                    song.imgUrl = cursor.getString(imgUrlIndex);
                    song.isFavorite = (cursor.getInt(isFavoriteIndex) == 1);
                    song.isNotInDevice = (cursor.getInt(isNotInDeviceIndex) == 1);
                    song.isPublicSong = (cursor.getInt(isPublicSongIndex) == 1);

                    song.bitrate = cursor.getInt(bitrateIndex);
                    song.duration = cursor.getInt(durationIndex);

                    songs.add(song);
                }
            }

            if (cursor != null) {
                cursor.close();
            }
            db.close();
            return songs;
        }
    }

    public static class SingerCache {
        public static void put(Context context, int type, List<SingerInfo> singers) {
            if (singers == null || singers.size() == 0) {
                return;
            }

            MusicDatabaseHelper helper = new MusicDatabaseHelper(context);
            SQLiteDatabase db = helper.getWritableDatabase();
            String whereClause = SingerCacheInfo.COLUMN_CACHE_TYPE + " = " + type;
            db.delete(SingerCacheInfo.NAME, whereClause, null); // delete old
                                                                // cache

            db.beginTransaction();
            try {
                ContentValues values = new ContentValues();
                for (SingerInfo singer : singers) {
                    values.clear();
                    values.put(SingerCacheInfo.COLUMN_SINGER_ID, singer.id);
                    values.put(SingerCacheInfo.COLUMN_SINGER_NAME, singer.name);
                    values.put(SingerCacheInfo.COLUMN_SINGER_IMG, singer.imgUrl);
                    values.put(SingerCacheInfo.COLUMN_COUNT, singer.count);
                    values.put(SingerCacheInfo.COLUMN_CACHE_TYPE, type);

                    db.insert(SingerCacheInfo.NAME, "0", values);
                }
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }

            db.close();
        }

        public static List<SingerInfo> get(Context context, int type) {
            ArrayList<SingerInfo> singers = new ArrayList<SingerInfo>();
            MusicDatabaseHelper helper = new MusicDatabaseHelper(context);
            SQLiteDatabase db = helper.getWritableDatabase();

            SQLiteQueryBuilder sqlbuilder = new SQLiteQueryBuilder();
            sqlbuilder.setTables(SingerCacheInfo.NAME);

            String selection = SingerCacheInfo.COLUMN_CACHE_TYPE + " = " + type;
            Cursor cursor = sqlbuilder.query(db, null /* projectionIn */, selection,
                    null/* selectionArgs */,
                    null/* groupBy */, null/* having */, null/* sortOrder */);

            if (cursor != null) {
                int idIndex = cursor.getColumnIndexOrThrow(SingerCacheInfo.COLUMN_SINGER_ID);
                int nameIndex = cursor.getColumnIndexOrThrow(SingerCacheInfo.COLUMN_SINGER_NAME);
                int imgIndex = cursor.getColumnIndexOrThrow(SingerCacheInfo.COLUMN_SINGER_IMG);
                int countIndex = cursor.getColumnIndexOrThrow(SingerCacheInfo.COLUMN_COUNT);

                while (cursor.moveToNext()) {
                    SingerInfo singer = new SingerInfo();
                    singer.id = cursor.getInt(idIndex);
                    singer.name = cursor.getString(nameIndex);
                    singer.imgUrl = cursor.getString(imgIndex);
                    singer.count = cursor.getInt(countIndex);

                    singers.add(singer);
                }
            }

            if (cursor != null) {
                cursor.close();
            }
            db.close();
            return singers;
        }
    }

}
